<template>
  <div class="container">
    <template>
      <!--添加老师-->
      <!--顶部操作表单-->
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item>
          <!--添加老师-->
          <el-button type="primary" @click="addStudent">添加老师</el-button>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="listQuery.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="listQuery.username" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item label="学院">
          <el-select v-model="listQuery.collegeId" :clearable="1===1" placeholder="请选择学院" @change="changeSelect">
            <el-option
              v-for="item in collegeOptions"
              :key="item.id"
              :label="item.collegeName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级">
          <el-select v-model="QueryForEdit.classId" :clearable="1===1" placeholder="请选择班级">
            <el-option
              v-for="item in classOptions"
              :key="item.id"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <!--添加老师表单-->
      <el-dialog title="添加老师" :visible.sync="addDialogFormVisible" top="30px" @close="OnClose">
        <el-form :model="QueryForEdit" ref="form"  :rules="rules">
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
            <el-input v-model="QueryForEdit.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号" :label-width="formLabelWidth" prop="username">
            <el-input v-model="QueryForEdit.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model="QueryForEdit.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="学院名称" :label-width="formLabelWidth" prop="collegeId">
            <el-select v-model="QueryForEdit.collegeId"  placeholder="请选择学院" @change="changeSelect">
              <el-option
                v-for="item in collegeOptions"
                :key="item.id"
                :label="item.collegeName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="班级名称" :label-width="formLabelWidth" prop="classId">
            <el-select  :clearable="1===1" v-model="QueryForEdit.classId" placeholder="请选择班级">
              <el-option
                v-for="item in classOptions"
                :key="item.id"
                :label="item.className"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="权限" :label-width="formLabelWidth" prop="power">
            <el-select v-model="QueryForEdit.power" placeholder="权限">
              <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.powerName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addDialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addClick('form')">确 定</el-button>
        </div>
      </el-dialog>
      <!--修改表单-->
      <el-dialog title="修改老师" :visible="editDialogFormVisible" top="30px" @close="OnClose" :show-close="false">
        <el-form :model="QueryForEdit" ref="form">
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
            <el-input v-model="QueryForEdit.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号" :label-width="formLabelWidth" prop="username">
            <el-input v-model="QueryForEdit.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model="QueryForEdit.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.phone" autocomplete="off"></el-input>
          </el-form-item>
          <!--        <el-form-item label="学院名称" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.collegeName" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="学院名称" :label-width="formLabelWidth" prop="collegeId">
            <el-select v-model="QueryForEdit.collegeId" placeholder="请选择学院" @change="changeSelect">
              <el-option
                v-for="item in collegeOptions"
                :key="item.id"
                :label="item.collegeName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <!--        <el-form-item label="班级名称" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.className" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="班级名称" :label-width="formLabelWidth" prop="classId">
            <el-select v-model="QueryForEdit.classId" :clearable="1===1"  placeholder="请选择班级">
              <el-option
                v-for="item in classOptions"
                :key="item.id"
                :label="item.className"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <!--        <el-form-item label="权限" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.powerName" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="权限" :label-width="formLabelWidth" prop="power">
            <el-select v-model="QueryForEdit.power" placeholder="权限">
              <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.powerName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editDialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addClick('form')">确 定</el-button>
        </div>
      </el-dialog>
      <!--教师列表-->
      <el-card class="box-card">
      <el-table
        ref="multipleTable"
        :data="tableData"
        v-loading="loading"
        border
        style="width: 100%">
        <el-table-column
          prop="username"
          label="账号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="age"
          label="年龄"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="powerName"
          label="角色"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="电话号码"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="collegeName"
          label="学院名称"
          width="180"
        >
        </el-table-column>
        <el-table-column
          label="班级名称"
          width="180"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.className==null">未分配</span>
            <span v-else>{{scope.row.className}}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle @click="handleEditClick(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="handleDeleteClick(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="listQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
      </el-card>
    </template>
  </div>
</template>

<script>
  import {getStudentList, addStudent, getTeacherList,deleteTea} from "../../api/teacher";
  import {geClassdropDownList, getCollegedropDownList, getRoledropDownList} from "../../api/common";
  import {deleteStu} from "../../api/student";

  export default {
    name: "",
    data() {
      return {
        loading:false,
        tableData: [],
        addDialogFormVisible: false,
        editDialogFormVisible:false,
        formLabelWidth: '120px',
        classOptions:[],
        collegeOptions:[],
        roleOptions:[],
        total:0,
        QueryForEdit:{
          name: '',
          username: '',
          password: '',
          age: '',
          collegeName: '',
          className: '',
          powerName: '',
          power: '',
          phone:'',
          email:'',
          classId: '',
          collegeId: '',
          typeForEdit: '',//添加是add，修改是edit
          id: ''
        },
        addForm: {
          name: '',
          username: '',
          password: '',
          age: '',
          collegeName: '',
          className: '',
          powerName: '',
          power: '',
          classId: '',
          collegeId: '',
          typeForEdit: '',//添加是add，修改是edit
          id: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        },
        listQuery: {
          page: 1,
          size: 10,
          username: '',
          name: '',
          classId: '',
          collegeId: '',
          id:''
        },
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ],
          username: [
            {required: true, message: '请输入账号', trigger: 'blur'}
          ],
          collegeId: [
            {required: true, message: '请选择学院', trigger: 'blur'}
          ],
          power: [
            {required: true, message: '请选择权限', trigger: 'blur'}
          ],
          age: [
            {required: true, message: '请输入年龄', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ],
          email: [
            {required: true, message: '请输入邮箱', trigger: 'blur'}
          ],
          phone: [
            {required: true, message: '请输入电话', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      // 获取老师列表
      getTeacherList() {
        this.loading=true
        getTeacherList(this.listQuery).then(res => {
          this.tableData = res.data.pageList
          this.total=res.data.total
          console.log(res)
          this.loading=false
        }).catch(err=>{
          this.loading=false
        })

      },
      //添加和修改的方法
      addClick(formName) {
        if (this.QueryForEdit.power == 1&&(this.QueryForEdit.classId==null||this.QueryForEdit.classId=='')) {
          this.$message.error("权限为班主任时，必须分配班级")
          return;
        }
        if (this.QueryForEdit.power == 2) {
          if (this.QueryForEdit.classId != null&&this.QueryForEdit.classId != '') {
            console.log(this.QueryForEdit.classId);
            this.$message.error("普通教师，不能分配班级")
            return;
          }


        }
        this.$refs[formName].validate((valid)=>{
          if (valid) {
            this.loading=true
            addStudent(this.QueryForEdit).then(res => {
              if (this.QueryForEdit.typeForEdit === 'add') {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                });
                this.editDialogFormVisible=false
              } else {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                });
                this.editDialogFormVisible=false
              }
              this.OnClose()
              this.getTeacherList()
              this.loading=false
            }).catch(err=>{
              this.loading=false
            })
          } else {
            this.$message.warning("请完善信息")
          }
        });

      },
      //关闭弹出框时
      handleDialogClose(){
        this.addForm={
          name: '',
          username: '',
          password: '',
          age: '',
          collegeName: '',
          className: '',
          powerName: '',
          power: '',
          classId: '',
          collegeId: '',
          typeForEdit: '',//添加是add，修改是edit
          id: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        }
      },
      //构造修改条件
      handleEditClick(param) {
        this.getdropDownListForCollege()
        this.QueryForEdit.name = param.name
        this.QueryForEdit.age = param.age
        this.QueryForEdit.username = param.username
        this.QueryForEdit.collegeName = param.collegeName
        this.QueryForEdit.id = param.id
        this.QueryForEdit.email=param.email
        this.QueryForEdit.phone=param.phone
        this.QueryForEdit.collegeId=param.collegeId
        this.QueryForEdit.classId=param.classId
        this.QueryForEdit.power=param.power
        this.QueryForEdit.typeForEdit = 'edit'
        this.editDialogFormVisible = true
        this.getDropdownListForRole()
        this.getdropDownListForClass(param.collegeId)
        this.getDropdownListForRole()
      },
      //构造添加条件
      addStudent() {
        this.addDialogFormVisible = true
        this.QueryForEdit.typeForEdit = 'add'
        this.getdropDownListForCollege()
        this.getDropdownListForRole()
      },
      //获取学院列表
      getdropDownListForCollege() {
        getCollegedropDownList(1484744242220589057)
          .then(res => {
            console.log(res)
            this.collegeOptions = res.data
          })
        this.getdropDownListForClass()
      },
      //获取班级列表
      getdropDownListForClass() {
        if (this.addDialogFormVisible == true||this.editDialogFormVisible==true) {
          geClassdropDownList(this.QueryForEdit.collegeId)
            .then(res => {
              console.log(res)
              this.classOptions = res.data
            });
        } else {
          geClassdropDownList(this.listQuery.collegeId)
            .then(res => {
              console.log(res)
              this.classOptions = res.data
            });
        }

      },
      //获取权限列表
      getDropdownListForRole() {
        getRoledropDownList().then(res => {
          console.log(res.data)
          this.roleOptions = res.data
          this.roleOptions.pop()
          this.roleOptions.pop()
          this.roleOptions.shift()
        })
      },
      //点击查询
      onSubmit() {
        this.getTeacherList()
      },
      changeSelect() {
        this.getdropDownListForClass()
      },
      //删除老师
      handleDeleteClick(param){
        console.log(param)
        this.loading=true
        this.listQuery.id=param.id
        deleteTea(this.listQuery).then(res=>{
          this.getTeacherList()
          this.loading=false
        }).catch(err=>{
          this.loading=true
        })
      },
      //分页相关
      handleSizeChange(val) {
        this.listQuery.size=val
        this.getStudentList()
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.listQuery.page=val
        this.getStudentList()
        console.log(`当前页: ${val}`);
      },
    OnClose(){
      this.addDialogFormVisible = false;
      this.QueryForEdit={
        name: '',
        username: '',
        password: '',
        age: '',
        collegeName: '',
        className: '',
        powerName: '',
        power: '',
        classId: '',
        collegeId: '',
        typeForEdit: '',//添加是add，修改是edit
        id: ''
      }
    }
    },
    created() {
      this.getTeacherList()
      this.getdropDownListForCollege()
    }
  }
</script>

<style scoped>
  .demo-form-inline{
    margin-top: 20px;
    margin-left: 10px;
  }
</style>
